package com.caocaog.market.dms.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caocaog.market.dms.dto.OrderSendDto;
import com.caocaog.market.dms.dto.QueryOrderDto;
import com.caocaog.market.dms.vo.OrderSendVo;
import com.caocaog.market.dms.vo.OrderValuationVo;
import com.caocaog.market.dms.dto.BindUserDto;
import com.caocaog.market.dms.dto.OrderValuationDto;
import com.caocaog.market.dms.vo.QueryOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

@Slf4j
@Component
public class DmsCommon {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 绑定曹操跑腿
     * @param bindUserDto
     * @return
     */
 public boolean bindUser(BindUserDto bindUserDto){
    String successData = DmsUtils.bindUer(bindUserDto.getUsername(), bindUserDto.getPassword());
       JSONObject successJSON = JSON.parseObject(successData);
       Integer code = successJSON.getInteger("code");
       if(code==200){
           JSONObject  resultObj=  successJSON.getJSONObject("result");
           String uid = resultObj.getString("uid");
           String key = resultObj.getString("key");
           if(Objects.nonNull(uid)&&Objects.nonNull(key)){
               bindUserDto.setKey(key);
               bindUserDto.setUid(uid);
               String redisKey ="caocaoToken:"+bindUserDto.getShopId();
               redisUtil.set(redisKey,bindUserDto,0);
               return true;
           }
       }
       log.error(String.valueOf(successJSON));
       return false;
    }

    /**
     * 获取已绑定的跑腿
     * @param shopId
     * @return
     */
  BindUserDto getBindUser(String shopId){
      String redisKey ="caocaoToken:"+shopId;
    BindUserDto bindUserDto = redisUtil.getEntity(redisKey, BindUserDto.class);
        if(Objects.isNull(bindUserDto)){
            throw new JeecgBootException("未查询到绑定信息");
        }
       return bindUserDto;
    }

    /**
     * 获取余额
     * @param shopId
     * @return
     */
    public BigDecimal getBalance(String shopId){
        BindUserDto bindUserDto = getBindUser(shopId);
           String key =bindUserDto.getKey();
           String uid=bindUserDto.getUid();
          String balanceData = DmsUtils.queryBalance(uid, key);

        JSONObject successJSON = JSON.parseObject(balanceData);
        Integer code = successJSON.getInteger("code");

        if(code==200){
          BigDecimal balance = successJSON.getBigDecimal("result");
          return balance;
        }
            return null;
    }

    /**
     * 订单计价
     * @param orderValuationDTO
     * @return
     */
    public OrderValuationVo orderValuation(OrderValuationDto orderValuationDTO){
        BindUserDto bindUserDto = getBindUser(orderValuationDTO.getShopId());
        String orderValuationData =   DmsUtils.orderValuation(orderValuationDTO, bindUserDto.getKey());
        JSONObject successJSON = JSON.parseObject(orderValuationData);
        Integer code = successJSON.getInteger("code");
        if(code==200){
            JSONObject  resultObj=  successJSON.getJSONObject("result");
        BigDecimal amount=     resultObj.getBigDecimal("amount")
                .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
        BigDecimal coupon =resultObj.getBigDecimal("coupon")
                .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
            BigDecimal distance =resultObj.getBigDecimal("distance")
                    .divide(BigDecimal.valueOf(1000),2,BigDecimal.ROUND_DOWN);
            OrderValuationVo vo=new OrderValuationVo();
                vo.setAmount(amount);
                vo.setCoupon(coupon);
                vo.setDistance(distance);

            return vo;
        }
        return null;
    }

    /**
     * 订单发单
     * @param orderSendDto
     * @return
     */
    public OrderSendVo orderSend(OrderSendDto orderSendDto){
        BindUserDto bindUserDto = getBindUser(orderSendDto.getShopId());
        String orderValuationData =   DmsUtils.orderSend(orderSendDto, bindUserDto.getKey());
        JSONObject successJSON = JSON.parseObject(orderValuationData);
        Integer code = successJSON.getInteger("code");
        if(code==200){
            JSONObject  resultObj=  successJSON.getJSONObject("result");
            BigDecimal amount = resultObj.getBigDecimal("amount")
                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
            BigDecimal coupon = resultObj.getBigDecimal("coupon")
                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
            BigDecimal payAmount = resultObj.getBigDecimal("payAmount")
                    .divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_DOWN);
            String ptOrder= resultObj.getString("orderId");
            String orderNo= resultObj.getString("caocaolaileOrderNo");
            OrderSendVo orderSendVo =new OrderSendVo();
            orderSendVo.setAmount(String.valueOf(amount));
            orderSendVo.setCoupon(String.valueOf(coupon));
            orderSendVo.setPayAmount(String.valueOf(payAmount));
            orderSendVo.setPtOrderNo(ptOrder);
            orderSendVo.setOrderNo(orderNo);

            return orderSendVo;
        }
        return null;
    }

    public QueryOrderVo orderQuery(QueryOrderDto queryOrderDto){
        try{
            BindUserDto bindUserDto = getBindUser(queryOrderDto.getShopId());
            String orderValuationData =
                    DmsUtils.orderQuery(queryOrderDto.getOrderNo(),bindUserDto.getKey());
            JSONObject successJSON = JSON.parseObject(orderValuationData);
            Integer code = successJSON.getInteger("code");
            if(code==200){
                JSONObject  resultObj=  successJSON.getJSONObject("result");
                String orderNo = resultObj.getString("orderNo");
                String driverId = resultObj.getString("driverId");
                String driverMobile = resultObj.getString("driverMobile");
                String driverName = resultObj.getString("driverName");
                String status = resultObj.getString("status");
                QueryOrderVo vo =new QueryOrderVo();
                vo.setDriverId(driverId);
                vo.setDriverMobile(driverMobile);
                vo.setDriverName(driverName);
                vo.setOrderNo(orderNo);
                vo.setStatus(status);
                return vo;
            }
        }catch (Exception e){
            return null;
        }

        return null;
    }

    public boolean orderCancel(QueryOrderDto queryOrderDto){
        BindUserDto bindUserDto = getBindUser(queryOrderDto.getShopId());
        String orderValuationData =
                DmsUtils.orderCancel(queryOrderDto.getOrderNo(),bindUserDto.getKey());
        JSONObject successJSON = JSON.parseObject(orderValuationData);
        Integer code = successJSON.getInteger("code");
        if(code==0){
                return true;
        }
        return false;
    }
}
